{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# getRegion(eeObject, bounds)\n",
    "Get the `region` for functions that need it as an argument (like Export.image)\n",
    "\n",
    "**params**\n",
    "- eeObject: any eeObject that has a geometry\n",
    "- bounds: return only bounds (rectangle). Defaults to `False`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geetools import tools"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bounded Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = ee.Image(ee.ImageCollection('COPERNICUS/S2').first())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[2.0823420055272694, 28.022371475142155],\n",
       "  [2.082262387713162, 28.022460994622374],\n",
       "  [1.7870267921380452, 28.020172100522007],\n",
       "  [1.7863863638768989, 28.019300827799174],\n",
       "  [1.7840385788154567, 28.010599213518702],\n",
       "  [1.7583696085580722, 27.9074708061136],\n",
       "  [1.7138484027983951, 27.72807167221268],\n",
       "  [1.711234871844144, 27.71720577206124],\n",
       "  [1.7112611391967587, 27.714974277442444],\n",
       "  [1.7119348947961275, 27.714682202980047],\n",
       "  [1.7139838289586986, 27.71406533734163],\n",
       "  [1.7676420145675962, 27.705345166014425],\n",
       "  [1.9106692751128727, 27.682352691622302],\n",
       "  [1.9653060360042989, 27.67356410080027],\n",
       "  [1.970992862852016, 27.67270366436133],\n",
       "  [1.9756637522563858, 27.672015966240707],\n",
       "  [2.0845963559712533, 27.656438224472712],\n",
       "  [2.0853071966042025, 27.65644288605324],\n",
       "  [2.085405603161776, 27.657703616668854],\n",
       "  [2.0823420055272694, 28.022371475142155]]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.geometry.getRegion(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unbounded Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "uimage = ee.Image.constant(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[-180.0, -90.0],\n",
       "  [180.0, -90.0],\n",
       "  [180.0, 90.0],\n",
       "  [-180.0, 90.0],\n",
       "  [-180.0, -90.0]]]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.geometry.getRegion(uimage)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Geometry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "pol = ee.Geometry.Polygon([[0,0], [10,10], [20,0], [0, 0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[0.0, 0.0], [20.0, 0.0], [10.0, 10.0], [0.0, 0.0]]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.geometry.getRegion(pol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[0.0, -4.4794226470436166e-14],\n",
       "  [20.0, -4.4794226470436166e-14],\n",
       "  [20.0, 10.000000000000025],\n",
       "  [0.0, 10.000000000000025],\n",
       "  [0.0, -4.4794226470436166e-14]]]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.geometry.getRegion(pol, True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "feat = ee.Feature(pol, {'test':'getRegion'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[0.0, 0.0], [20.0, 0.0], [10.0, 10.0], [0.0, 0.0]]]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.geometry.getRegion(feat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FeatureCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "fc = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filterMetadata('wld_rgn', 'equals', 'South America')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[-109.45547482907725, -55.915443407767505],\n",
       "  [-28.842971814946278, -55.915443407767505],\n",
       "  [-28.842971814946278, 13.387999563952137],\n",
       "  [-109.45547482907725, 13.387999563952137],\n",
       "  [-109.45547482907725, -55.915443407767505]]]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.geometry.getRegion(fc, True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
